// Authors: Matthew DeNovio
#include "Image.h"

using namespace std;

// Constructors
Image::Image(void)
{
	_pImage = NULL;
}

Image::Image(string path)
{
	LoadImage(path);
}


// Deconstructor
Image::~Image(void)
{
	if(_pImage != NULL)
	{
		free(*_pImage);		// delete the arrays in the 2nd dimension
		free(_pImage);		// delete the first dimension
	}	
}



// functions


// Load the image from the File
#pragma region LoadImage

void Image::LoadImage(string path)
{
	ifstream myfile (path);
	string data;

	// if it's open
	if(myfile.is_open())
	{
		myfile >> data;
		_startingPoint.x = atoi(data.c_str());

		myfile >> data;
		_startingPoint.y = atoi(data.c_str());

		
		myfile >> data;
		_width = atoi(data.c_str());

		myfile >> data;
		_height = atoi(data.c_str());


		// Allocate Space for Image
		_pImage = (Pixel **)(malloc(sizeof(Pixel*) * _height));
		*_pImage = (Pixel *)(malloc(sizeof(Pixel) * _height * _width));
		for(U16 r = 1; r < _height; r++)
			*(_pImage + r) = *_pImage + _width * r;


		// Fill Image
		for (U16 i = 0; i < _height; i++) {

			myfile >> data;

			for (U16 j = 0; j < _width; j++)
			{
				
				_pImage[i][j] = data[j] - 33;
			}
		}
	}

	myfile.close();
}

#pragma endregion


// Draw Image
#pragma region DrawImage

void Image::DrawImage(Point cursorLoc, Camera * pGameCamera)
{

	for (U16 k = 0; k < _height; k++)
	{

		pGameCamera->setCursorPosition(cursorLoc.x, cursorLoc.y);

		for (U16 l = 0; l < _width; l++)
		{
			// Draw Tile
			cout << _pImage[k][l];
		}

		++cursorLoc.y;
	}
}

#pragma endregion

// Draws particular line of Image
void Image::DrawHorizontalLine(Point Loc, Camera * pGameCamera)
{
	U8 localYLine = Loc.y % pGameCamera->TILE_DIM;

	pGameCamera->setCursorPosition(Loc.x, Loc.y);

	for (U16 l = 0; l < _width; l++)
	{
		if(pixelNotDrawn(Loc, _pImage[localYLine][l]._color))
		{
			// Draw Tile
			cout << _pImage[localYLine][l];
		}
	}

}
void Image::DrawVerticalLine(Point Loc, Camera * pGameCamera)
{
	U8 localXLine = Loc.x % pGameCamera->TILE_DIM;

	for (U16 k = 0; k < _height; k++)
	{
		if(pixelNotDrawn(Loc, _pImage[k][localXLine]._color))
		{
			pGameCamera->setCursorPosition(Loc.x, Loc.y);
			// Draw Tile
			cout << _pImage[k][localXLine];
		}
		++Loc.y;
	}
}

bool Image::pixelNotDrawn(const Point& Loc, U8 pixelColor)
{

	//return (GetPixel(GetStdHandle ( STD_OUTPUT_HANDLE ),Loc.x,Loc.y))


	return 1;
}

// Operator Overloads


/*
// Add an Image to the Img
void Image::operator += (Image &img)
{
	for (U16 i = img.startingPoint->y; i < img.startingPoint->y + img.height; i++)
		for (U16 j = img.startingPoint->x; j < img.startingPoint->x + img.width; j++)
			image[i + img.startingPoint->y][j + img.startingPoint->x] = img.image[i][j];

}


// Remove the img from the image
void Image::operator -= (Image &img)
{
	for (U16 i = img.startingPoint->y; i < img.startingPoint->y + img.height; i++)
		for (U16 j = img.startingPoint->x; j < img.startingPoint->x + img.width; j++)
			image[i + img.startingPoint->y][j + img.startingPoint->x] = I8(171);
}
*/